<?php
/**
 *
 * @author yupoxiong<i@yupoxiong.com>
 */

declare (strict_types = 1);

namespace app\api\controller;
use app\common\model\Company;
use app\common\model\Notice;
use app\common\model\Project;
use app\common\model\ProjectLog;
use app\common\model\Slide;
use app\common\model\User;
use hg\apidoc\annotation as Apidoc;
use think\response\Json;

/**
 * @Apidoc\Title("首页")
 * @Apidoc\Group("index")
 */
class IndexController extends ApiBaseController {
	protected array $loginExcept = [
		'api/index/screen',
		'api/index/time',
	];
	/**
	 * @Apidoc\Title("首页")
	 * @Apidoc\Desc("首页")
	 * @Apidoc\Method("GET")
	 * @Apidoc\Url("/api/index/index")
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function index(): Json {
		$slide = Slide::where('status', 1)->order('sort', 'asc')->select()->toArray();
		$notice = Notice::where('status', 1)->whereDay('create_time')->order('id', 'desc')->select()->toArray();
		$company = Company::find($this->user->company_id);

		$project = Project::with(['cate'])->whereIn('cate_id', $company['cate'])->whereNotIn('status', [2, 3])->order('start_time', 'asc')->select()->toArray();
		$data = [
			'slide' => $slide,
			'notice' => $notice,
			'project' => $project,
		];
		return api_success($data);
	}
	public function screen() {
		if (isset($this->param['cate']) && $this->param['cate'] == '-1') {
			unset($this->param['cate']);
		} else {
			$this->param['cate'] = [$this->param['cate']];
		}

		$project_list = Project::with(['cate'])->whereIn('status', [0, 1])->order('start_time')->select();
		$company_list = Company::scope('ApiWhere', $this->param)->select()
			->each(function ($item, $key) {
				$user = User::where('company_id', $item['id'])->whereTime('live_time', '-10 second')->findOrEmpty();
				if ($user->isEmpty()) {
					$item['is_live'] = false;
				} else {
					$item['is_live'] = true;
				}

			})
			->append(['cate_list']);
		$user_id_list = ProjectLog::where('status', 2)->column('user_id');
		$company_id_list = User::whereIn('id', $user_id_list)->column('company_id');
		$company_success_list = ProjectLog::with(['user', 'project'])->where('status', 2)->order('id', 'desc')->select()->append(['user.company']);

		$company_error_list = Company::where('not_open', '<>', 0)->order('not_open', 'desc')->select();
		$data = [
			'company_list' => $company_list,
			'project_list' => $project_list,
			'company_success_list' => $company_success_list,
			'company_error_list' => $company_error_list,
		];
		return api_success($data);
	}
	public function time() {
		return api_success(['time' => time() * 1000]);
	}
	public function live() {
		User::where('id', $this->uid)->update(['live_time' => time()]);
	}
	/**
	 * @Apidoc\Title("所有未开始的")
	 * @Apidoc\Desc("所有未开始的")
	 * @Apidoc\Method("GET")
	 * @Apidoc\Url("/api/index/all_project")
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function all_project() {
		$project = Project::with(['cate'])->where('status', 0)->order('start_time', 'asc')->select()->toArray();
		$data = [
			'project' => $project,
		];
		return api_success($data);
	}
}
